//
//  KKNavigationViewController.swift
//  goodProducts
//
//  Created by KKING on 16/6/22.
//  Copyright © 2016年 kpc. All rights reserved.
//

import UIKit

class KKNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //解决右滑返回手势的问题
        //存在bug 在最顶级的页面 右滑返回手势还会响应
        self.interactivePopGestureRecognizer?.delegate = self
        
        automaticallyAdjustsScrollViewInsets = false
        let navBar = navigationBar
        navBar.barTintColor = COLOR_NAV_BG
//        navBar.translucent = false
//        navBar.barStyle = UIBarStyle.Black
//        navBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
//        navBar.shadowImage = UIImage()
        
        // 导航栏背景色
        navigationBar.titleTextAttributes = [
            NSFontAttributeName : baseFont(size: 18),
            NSForegroundColorAttributeName : COLOR_NAV_ITEM_NORMAL
        ]
    }
    
    
    
    
        
    //重写push 方法
    //HMBaseNavController(rootViewController: vc) ==> 内部会进行push操作 ==> 压栈
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        //变量名称 和 方法名称 最好不要重名
        //判断是否是顶级页面
//        var backtitle = "返回"
        if childViewControllers.count > 0 {
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.leftItem(normalImage: "top_navigation_back_normal", highlightedImage: "top_navigation_back_normal", target: self, action: #selector(popToFrontVC(button:)))
            viewController.hidesBottomBarWhenPushed = true
        } else {
            viewController.hidesBottomBarWhenPushed = false
        }
//            if childViewControllers.count == 1 {
//                //设置返回按钮的title = 顶级页面的title
//                backtitle = childViewControllers.first?.title ?? "返回"
//            }
//            let back = UIBarButtonItem(imageName: "navigation_back_normal", title: backtitle, target: self, action: #selector(popToFrontVC))
//            viewController.navigationItem.leftBarButtonItem = back
//        }
        
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func popToFrontVC(button: UIBarButtonItem) {
        //pop
        popViewController(animated: true)
    }
    
    //实现手势协议方法
    //一般协议方法返回值 为 bool类型 表示 如果返回 true 就表示对象能够正常工作
    //如果返回false  对象就不能正常工作
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
    }
}
